// 逾期收款mock数据
let overdueReceivableId = 8; // 用于生成新ID

const mockOverdueReceivables = [
  {
    id: 1,
    customerCode: 'KH101',
    serviceProject: '代理记账+报税',
    customerName: '北京科技发展公司',
    customerRating: 'A',
    contactPerson: '张伟',
    contactPhone: '13800138101',
    paymentMonth: '2024-02',
    servicePeriod: '2024年1月-2024年12月',
    receivableAmount: 15000,
    memberEnterprise: '是',
    area: '北京',
    salesperson: '李四',
    accountant: '王五',
    followWechat: '已关注',
    receivedAmount: 5000,
    receivedUpTo: '2024-01',
    paymentStatus: '部分收款',
    lastPushTime: '2024-03-20 10:30:00',
    receiptNumber: 'SP2024101',
    assistant: '赵六',
    paymentRemark: '客户承诺3月底前通过银行转账补齐剩余款项，已催收3次',
    dueDate: '2024-02-28',
    overdueDays: 21,
  },
  {
    id: 2,
    customerCode: 'KH102',
    serviceProject: '财务咨询',
    customerName: '上海贸易集团',
    customerRating: 'B+',
    contactPerson: '李明华',
    contactPhone: '13900139102',
    paymentMonth: '2024-01',
    servicePeriod: '2024年1月-2024年6月',
    receivableAmount: 12000,
    memberEnterprise: '否',
    area: '上海',
    salesperson: '王七',
    accountant: '陈八',
    followWechat: '未关注',
    receivedAmount: 0,
    receivedUpTo: '',
    paymentStatus: '未收款',
    lastPushTime: '2024-03-18 14:20:00',
    receiptNumber: '',
    assistant: '刘九',
    paymentRemark: '财务部门反馈资金紧张，申请延期至下月15日，需要总监审批',
    dueDate: '2024-01-31',
    overdueDays: 49,
  },
  {
    id: 3,
    customerCode: 'KH103',
    serviceProject: '税务筹划',
    customerName: '深圳创新企业',
    customerRating: 'A-',
    contactPerson: '王华强',
    contactPhone: '13700137103',
    paymentMonth: '2024-02',
    servicePeriod: '2024年1月-2024年12月',
    receivableAmount: 18000,
    memberEnterprise: '是',
    area: '深圳',
    salesperson: '张十',
    accountant: '李十一',
    followWechat: '已关注',
    receivedAmount: 9000,
    receivedUpTo: '2024-01',
    paymentStatus: '部分收款',
    lastPushTime: '2024-03-19 09:15:00',
    receiptNumber: 'SP2024102',
    assistant: '陈十二',
    paymentRemark: '客户要求分期付款，已同意分3期，第一期已到账5000元',
    dueDate: '2024-02-29',
    overdueDays: 20,
  },
  {
    id: 4,
    customerCode: 'KH104',
    serviceProject: '审计服务',
    customerName: '广州制造集团',
    customerRating: 'A+',
    contactPerson: '陈丽华',
    contactPhone: '13600136104',
    paymentMonth: '2024-01',
    servicePeriod: '2024年1月-2024年12月',
    receivableAmount: 25000,
    memberEnterprise: '是',
    area: '广州',
    salesperson: '李四',
    accountant: '王五',
    followWechat: '已关注',
    receivedAmount: 15000,
    receivedUpTo: '2024-01',
    paymentStatus: '部分收款',
    lastPushTime: '2024-03-21 16:45:00',
    receiptNumber: 'SP2024103',
    assistant: '赵六',
    paymentRemark: '联系人更换，新财务负责人表示尽快处理，预计本周内到账',
    dueDate: '2024-01-31',
    overdueDays: 49,
  },
  {
    id: 5,
    customerCode: 'KH105',
    serviceProject: '代理记账',
    customerName: '杭州电商平台',
    customerRating: 'B',
    contactPerson: '刘强东',
    contactPhone: '13500135105',
    paymentMonth: '2024-02',
    servicePeriod: '2024年1月-2024年6月',
    receivableAmount: 8000,
    memberEnterprise: '否',
    area: '杭州',
    salesperson: '王七',
    accountant: '陈八',
    followWechat: '未关注',
    receivedAmount: 0,
    receivedUpTo: '',
    paymentStatus: '未收款',
    lastPushTime: '2024-03-17 11:20:00',
    receiptNumber: '',
    assistant: '刘九',
    paymentRemark: '公司经营困难，已启动法务程序，建议转交风控部门处理',
    dueDate: '2024-02-28',
    overdueDays: 21,
  },
  {
    id: 6,
    customerCode: 'KH106',
    serviceProject: '财务培训',
    customerName: '北京教育集团',
    customerRating: 'C+',
    contactPerson: '张敏华',
    contactPhone: '13400134106',
    paymentMonth: '2024-01',
    servicePeriod: '2024年1月-2024年8月',
    receivableAmount: 15000,
    memberEnterprise: '否',
    area: '北京',
    salesperson: '张十',
    accountant: '李十一',
    followWechat: '未关注',
    receivedAmount: 5000,
    receivedUpTo: '2024-01',
    paymentStatus: '部分收款',
    lastPushTime: '2024-03-16 08:30:00',
    receiptNumber: 'SP2024104',
    assistant: '陈十二',
    paymentRemark: '客户反馈发票有误，重新开具后承诺立即付款',
    dueDate: '2024-01-31',
    overdueDays: 49,
  },
  {
    id: 7,
    customerCode: 'KH107',
    serviceProject: '工商年检',
    customerName: '成都软件公司',
    customerRating: 'A-',
    contactPerson: '王磊强',
    contactPhone: '13300133107',
    paymentMonth: '2024-02',
    servicePeriod: '2024年1月-2024年12月',
    receivableAmount: 11000,
    memberEnterprise: '是',
    area: '成都',
    salesperson: '李四',
    accountant: '王五',
    followWechat: '已关注',
    receivedAmount: 0,
    receivedUpTo: '',
    paymentStatus: '未收款',
    lastPushTime: '2024-03-22 15:20:00',
    receiptNumber: '',
    assistant: '赵六',
    paymentRemark: '已发送正式催收函，客户电话无人接听，邮件暂无回复',
    dueDate: '2024-02-29',
    overdueDays: 20,
  },
];

export default {
  // 获取逾期收款列表
  'GET /api/overdue-receivables': (req, res) => {
    const { status, current = 1, pageSize = 10, ...params } = req.query;
    
    // 根据状态过滤
    let filteredData = mockOverdueReceivables;
    if (status === '未收款') {
      filteredData = mockOverdueReceivables.filter(item => item.paymentStatus === '未收款');
    } else if (status === '部分收款') {
      filteredData = mockOverdueReceivables.filter(item => item.paymentStatus === '部分收款');
    } else if (status === '已收齐') {
      filteredData = mockOverdueReceivables.filter(item => item.paymentStatus === '已收齐');
    }
    
    // 搜索过滤
    if (params.customerName) {
      filteredData = filteredData.filter(item => 
        item.customerName.includes(params.customerName)
      );
    }
    if (params.customerCode) {
      filteredData = filteredData.filter(item => 
        item.customerCode.includes(params.customerCode)
      );
    }
    if (params.contactPerson) {
      filteredData = filteredData.filter(item => 
        item.contactPerson.includes(params.contactPerson)
      );
    }
    
    // 分页
    const startIndex = (current - 1) * pageSize;
    const endIndex = startIndex + parseInt(pageSize);
    const pagedData = filteredData.slice(startIndex, endIndex);
    
    res.json({
      success: true,
      data: {
        list: pagedData,
        total: filteredData.length,
        current: parseInt(current),
        pageSize: parseInt(pageSize),
      },
    });
  },

  // 创建逾期收款记录
  'POST /api/overdue-receivables': (req, res) => {
    const newRecord = {
      id: overdueReceivableId++,
      ...req.body,
      paymentStatus: '未收款',
      receivedAmount: 0,
      receivedUpTo: '',
      receiptNumber: '',
    };
    mockOverdueReceivables.unshift(newRecord);
    
    res.json({
      success: true,
      data: newRecord,
      message: '逾期收款记录创建成功',
    });
  },

  // 更新逾期收款记录
  'PUT /api/overdue-receivables/:id': (req, res) => {
    const { id } = req.params;
    const index = mockOverdueReceivables.findIndex(item => item.id == id);
    
    if (index !== -1) {
      mockOverdueReceivables[index] = { ...mockOverdueReceivables[index], ...req.body };
      res.json({
        success: true,
        data: mockOverdueReceivables[index],
        message: '逾期收款记录更新成功',
      });
    } else {
      res.status(404).json({
        success: false,
        message: '逾期收款记录不存在',
      });
    }
  },

  // 删除逾期收款记录
  'DELETE /api/overdue-receivables/:id': (req, res) => {
    const { id } = req.params;
    const index = mockOverdueReceivables.findIndex(item => item.id == id);
    
    if (index !== -1) {
      mockOverdueReceivables.splice(index, 1);
      res.json({
        success: true,
        message: '逾期收款记录删除成功',
      });
    } else {
      res.status(404).json({
        success: false,
        message: '逾期收款记录不存在',
      });
    }
  },
}; 